package view;

import java.text.DecimalFormat;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JList;

import utils.Dish;

public class FacturePreview extends JList implements Observer{
	
	private static final long serialVersionUID = 1L;
	
	public FacturePreview(Observable obs){
		obs.addObserver(this);
	}
	
	public void update(Observable obs, Object obj) {
		ArrayList<Dish> dishesList = (ArrayList<Dish>)obj;
		Iterator<Dish> dishesListIt = dishesList.iterator();
				
		DecimalFormat pricesFormat = new DecimalFormat("0.00");
		float totalPrice = 0;
		removeAll();
		
		ButtonGroup dishesListButtonGroup = new ButtonGroup();
		while(dishesListIt.hasNext()){
			Dish dish = dishesListIt.next();
			
			JRadioButton listElement = new JRadioButton(dish.getName()+" : "+dish.getPrice()+" $");
			listElement.setBounds(0, dishesList.indexOf(dish)*20, 300, 20);
			listElement.setSelected((dishesList.indexOf(dish) == 0) ? true: false);
			listElement.setName(""+dishesList.indexOf(dish));
			dishesListButtonGroup.add(listElement);
			add(listElement);
			totalPrice += dish.getPrice();
			
			if(dishesList.indexOf(dish) == dishesList.size()-1){
				JLabel totalPriceLabel = new JLabel("Total price : "+pricesFormat.format(totalPrice)+" $");
				totalPriceLabel.setBounds(27, dishesList.size()*20, 300, 20);
				add(totalPriceLabel);
			}
		}
		
		repaint();
	}
}
